﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Web.UI;

namespace Util
{
	/// <summary>
	/// 每个业务逻辑类都要实现check方法，即AU属性验证，不合格不通过。
	/// 所有方法都可重写，override。
	/// 实例化所需的Page对象和DAL可以用默认的也可以在实例化时传递进来。
	/// 通用方法可以添加在此类中，便于维护
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public abstract class Bll<T> where T : EntityObject
	{
		protected Page _page;
		protected Dal<T> _dal;

		public Bll()
		{
			this._page = null;
			this._dal = new Dal<T>();
		}
		public Bll(Page _page)
		{
			this._page = _page;
			this._dal = new Dal<T>();
		}
		public Bll(Dal<T> _dal)
		{
			this._page = null;
			this._dal = _dal;
		}
		public Bll(Page _page, Dal<T> _dal)
		{
			this._page = _page;
			this._dal = _dal;
		}
		/// <summary>
		/// 检验实体类中的各个属性是否合格
		/// </summary>
		/// <param name="entity">检验实体</param>
		/// <returns></returns>
		public abstract bool Check(T entity);
		public virtual int GetCount(Func<T, bool> func)
		{
			return _dal.GetCount(func);
		}
		public virtual bool Add(T entity)
		{
			if (!Check(entity))
				return false;
			int res = _dal.Add(entity);
			if (res == 1)
			{
				UtilWeb.Message(_page, KoInfo.OperateSuccess, null);
				return true;
			}
			else
			{
				UtilWeb.Message(_page, KoInfo.OperateFailure, null);
				return false;
			}
		}
		public virtual bool Add(List<T> list)
		{
			if (list.Count < 1)
				return false;
			foreach (T item in list)
				if (!Check(item))
					return false;

			int res = _dal.Add(list);
			if (res == list.Count)
			{
				UtilWeb.Message(_page, KoInfo.OperateSuccess, null);
				return true;
			}
			else
			{
				UtilWeb.Message(_page, KoInfo.OperateFailure, null);
				return false;
			}
		}
		public virtual bool Update(T entity)
		{
			if (!Check(entity))
				return false;
			int res = _dal.Update(entity);
			if (res == 1)
			{
				UtilWeb.Message(_page, KoInfo.OperateSuccess, null);
				return true;
			}
			else
			{
				UtilWeb.Message(_page, KoInfo.OperateFailure, null);
				return false;
			}
		}
		public virtual bool Delete(int id)
		{
			int res = _dal.Delete(id);
			if (res == 1)
			{
				UtilWeb.Message(_page, KoInfo.OperateSuccess, null);
				return true;
			}
			else
			{
				UtilWeb.Message(_page, KoInfo.OperateFailure, null);
				return false;
			}
		}
		public virtual bool Delete(Func<T, bool> func)
        {
            int count = GetCount(func);
			int res = _dal.Delete(func);
			if (res == count)
			{
                UtilWeb.Message(_page, KoInfo.OperateSuccess, null);
				return true;
			}
			else
			{
				UtilWeb.Message(_page, KoInfo.OperateFailure, null);
				return false;
			}
		}
		public virtual T GetSingle(int id)
		{
			return _dal.GetSingle(id);
		}
		public virtual T GetSingle(Func<T, bool> func)
		{
			return _dal.GetSingle(func);
		}
		public virtual T GetSingleRelated(Func<T, bool> func, string include)
		{
			return _dal.GetSingleRelated(func, include);
		}
		public virtual T GetSingleRelated(Func<T, bool> func, string[] includes)
		{
			return _dal.GetSingleRelated(func, includes);
		}
		public virtual IList<T> GetList(Func<T, bool> func)
		{
			return _dal.GetList(func);
		}
		public virtual IList<T> GetList(Func<T, bool> func, bool asce1, Func<T, object> order1)
		{
			return _dal.GetList(func, asce1, order1);
		}
		public virtual IList<T> GetList(Func<T, bool> func, int index, int size)
		{
			return _dal.GetList(func, index, size);
		}
		public virtual IList<T> GetList(Func<T, bool> func, bool asce1, Func<T, object> order1, int index, int size)
		{
			return _dal.GetList(func, asce1, order1, index, size);
		}
		public virtual IList<T> GetList(Func<T, bool> func, bool asce1, Func<T, object> order1, bool asce2, Func<T, object> order2, int index, int size)
		{
			return _dal.GetList(func, asce1, order1, asce2, order2, index, size);
		}
		public virtual IList<T> GetList(Func<T, bool> func, IDictionary<Func<T, object>, bool> dic, int index, int size)
		{
			return _dal.GetList(func, dic, index, size);
		}
		public virtual IList<T> GetListRelated(Func<T, bool> func, string include)
		{
			return _dal.GetListRelated(func, include);
		}
		public virtual IList<T> GetListRelated(Func<T, bool> func, string[] includes)
		{
			return _dal.GetListRelated(func, includes);
		}
       
	}
}